home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / gfx / show / MysticView0_68.lha / mysticview / source / guigfx.c < prev    next >
C/C++ Source or Header  |  1996-12-29  |  10KB  |  422 lines

  1. /*********************************************************************
  2. ----------------------------------------------------------------------
  3.  
  4.     $VER: guigfx.c 11.0
  5.     ©1996 TEK neoscientists
  6.  
  7.     Functions for scaling, color-adapting, and displaying
  8.     chunky graphics.
  9.  
  10.     This is a simple interface between render.library
  11.     and graphics.library.
  12.  
  13.     There will be a seperate library for this purpose, soon.
  14.  
  15. ----------------------------------------------------------------------
  16. *********************************************************************/
  17.  
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #include "render/render.h"
  23.  
  24. #include "utility/tagitem.h"
  25. #include "graphics/gfx.h"
  26. #include "graphics/view.h"
  27. #include "exec/memory.h"
  28.  
  29. #include "proto/render.h"
  30. #include "proto/graphics.h"
  31. #include "proto/exec.h"
  32.  
  33.  
  34. #include "guigfx.h"
  35. #include "global.h"
  36.  
  37. /*------------------------------------------------------------------*/
  38.  
  39. #define MIN(a,b)        ((a) < (b) ? (a) : (b))
  40. #define MAX(a,b)        ((a) > (b) ? (a) : (b))
  41. #define    FREEVECPTR(a)    if(a){FreeRenderVec(a);a=NULL;}
  42.  
  43. /*------------------------------------------------------------------*/
  44.  
  45. /*********************************************************************
  46. ----------------------------------------------------------------------
  47.  
  48.     psm = CreatePenShareMap ( void )    
  49.  
  50.     create and set-up a pensharemap.
  51.  
  52. ----------------------------------------------------------------------
  53. *********************************************************************/
  54.  
  55. struct PenShareMap *CreatePenShareMap(void)
  56. {
  57.     APTR rmh, histo;
  58.     struct PenShareMap *psm;
  59.     
  60.     if (rmh = CreateRMHandler(RND_MemType,RMHTYPE_POOL,TAG_DONE))
  61.     {
  62.         if (psm = (struct PenShareMap *) AllocRenderVec(rmh,sizeof(struct PenShareMap)))
  63.         {
  64.             if (histo = CreateHistogram(RND_RMHandler,rmh,RND_HSType,HSTYPE_18BIT,TAG_DONE))
  65.             {
  66.                 psm -> histogram = histo;
  67.                 psm -> memhandler = rmh;
  68.  
  69.                 psm -> mainpalette = 0;
  70.                 psm -> mainnumcolors = 0;
  71.                 psm -> mainpentab = 0;
  72.  
  73.                 psm -> basepalette = 0;
  74.                 psm -> basenumcolors = 0;
  75.                 psm -> basepentab = 0;
  76.  
  77.                 return psm;
  78.             }
  79.  
  80.             FreeRenderVec(psm);
  81.         }
  82.  
  83.         DeleteRMHandler(rmh);
  84.     }
  85.  
  86.     return NULL;
  87. }
  88.  
  89. /********************************************************************/
  90.  
  91.  
  92.  
  93.  
  94. /*********************************************************************
  95. ----------------------------------------------------------------------
  96.  
  97.     RemapChunkyArray ( source,palette,dest,width,height,pensharemap )    
  98.  
  99.     remap a chunky array to an obtained pensharemap.
  100.     
  101.     This is a tricky algorythm to render
  102.     a chunky image to a pensharemap.
  103.  
  104. ----------------------------------------------------------------------
  105. *********************************************************************/
  106.  
  107. void RemapChunkyArray ( UBYTE *source, APTR palette, 
  108.             UWORD width, UWORD height, UBYTE *dest, struct PenShareMap *psm)
  109. {
  110.     APTR realpalette;
  111.     int i;
  112.     ULONG rgb[3];
  113.     UBYTE pentab[256];
  114.     UBYTE indextab[256];
  115.  
  116.     if(psm->colormap)
  117.     {
  118.  
  119.         if(realpalette = CreatePalette(RND_HSType,HSTYPE_18BIT,TAG_DONE))
  120.         {
  121.             for(i=0; i<psm->mainnumcolors; ++i)
  122.             {
  123.                 GetRGB32(psm->colormap,psm->mainpentab[i],1,rgb);                                
  124.  
  125.                 ImportPalette(realpalette,rgb,1,
  126.                                     RND_PaletteFormat,PALFMT_RGB32,
  127.                                     RND_FirstColor,i,
  128.                                     TAG_DONE);
  129.                 indextab[i]=i;
  130.             }
  131.  
  132.             CreatePenTable(indextab,psm->mainpalette,
  133.                     psm->mainnumcolors,1,realpalette,pentab,
  134.                     RND_PenTable,psm->mainpentab,TAG_DONE);
  135.  
  136.             ConvertChunky(    source,palette,width,height,dest,
  137.                             psm->mainpalette,RND_PenTable,pentab,TAG_DONE );
  138.  
  139.             DeletePalette(realpalette);
  140.         }
  141.     }
  142. }
  143.  
  144.  
  145. /*********************************************************************
  146. ----------------------------------------------------------------------
  147.  
  148.     void ReleasePenTab ( pentab, colormap, numcolors )
  149.  
  150.     (internal)
  151.  
  152. ----------------------------------------------------------------------
  153. *********************************************************************/
  154.  
  155. void ReleasePenTab ( UBYTE *pentab, struct ColorMap *cm, UWORD numcolors )
  156. {
  157.     int i;
  158.     
  159.     for(i=0; i<numcolors; ++i)
  160.     {
  161.         ReleasePen(cm,(ULONG)pentab[i]);
  162.     }
  163. }
  164.  
  165.  
  166. /*********************************************************************
  167. ----------------------------------------------------------------------
  168.  
  169.     obtained = ObtainPenTab ( palette, colormap, pentab, numcolors, precision )
  170.  
  171.     (internal)
  172.  
  173. ----------------------------------------------------------------------
  174. *********************************************************************/
  175.  
  176. BOOL ObtainPenTab (    APTR palette, struct ColorMap *cm, UBYTE *pentab,
  177.                     UWORD numcolors, ULONG precision )
  178. {
  179.     int i;
  180.     LONG pen;
  181.     ULONG rgb[3];
  182.     UWORD count = 0;
  183.  
  184.     for(i=0; i < numcolors; ++i)
  185.     {
  186.         ExportPalette(palette,rgb,    RND_PaletteFormat,PALFMT_RGB32,
  187.                                     RND_FirstColor,i,
  188.                                     RND_NumColors,1,
  189.                                     TAG_DONE);
  190.  
  191.         pen = ObtainBestPen(cm,rgb[0],rgb[1],rgb[2],OBP_Precision,precision,TAG_DONE);
  192.         if (pen == -1)
  193.         {
  194.             ReleasePenTab(pentab,cm,count);
  195.             return FALSE;
  196.         }
  197.         else
  198.         {
  199.             pentab[i] = (UBYTE) pen;
  200.             count++;
  201.         }
  202.     }
  203.  
  204.     return TRUE;
  205. }
  206.  
  207.  
  208. /*********************************************************************
  209. ----------------------------------------------------------------------
  210.  
  211.     void ReleasePenShareMap ( psm )    
  212.  
  213.     release a pensharemap from its ColorMap.
  214.  
  215. ----------------------------------------------------------------------
  216. *********************************************************************/
  217.  
  218. void ReleasePenShareMap ( struct PenShareMap *psm )
  219. {
  220.     if(psm->basenumcolors && psm->basepentab && psm->colormap)
  221.     {
  222.         ReleasePenTab(psm->basepentab,psm->colormap,psm->basenumcolors);
  223.     }
  224.  
  225.     if(psm->mainnumcolors && psm->mainpentab && psm->colormap)
  226.     {
  227.         ReleasePenTab(psm->mainpentab,psm->colormap,psm->mainnumcolors);
  228.     }
  229.  
  230.     FREEVECPTR(psm->mainpentab);
  231.  
  232.     if(psm->mainpalette)
  233.     {
  234.         DeletePalette(psm->mainpalette);
  235.         psm->mainpalette = 0;
  236.     }
  237.  
  238.     FREEVECPTR(psm->basepentab);
  239.  
  240.     if(psm->basepalette)
  241.     {
  242.         DeletePalette(psm->basepalette);
  243.         psm->basepalette = 0;
  244.     }
  245.  
  246.     psm->mainnumcolors = 0;
  247.     psm->basenumcolors = 0;
  248.     psm->colormap = NULL;
  249.     
  250. }
  251.  
  252.  
  253. /*********************************************************************
  254. ----------------------------------------------------------------------
  255.  
  256.     DeletePenShareMap(psm)
  257.  
  258.     annihilate pensharemap (and release it, if obtained before).
  259.  
  260. ----------------------------------------------------------------------
  261. *********************************************************************/
  262.  
  263. void DeletePenShareMap(struct PenShareMap *psm)
  264. {
  265.     APTR rmh = psm -> memhandler;
  266.  
  267.     ReleasePenShareMap(psm);
  268.     DeleteHistogram(psm->histogram);
  269.     FreeRenderVec(psm);
  270.     DeleteRMHandler(rmh);
  271.  
  272. }
  273.  
  274.  
  275. /*********************************************************************
  276. ----------------------------------------------------------------------
  277.  
  278.     success = ObtainPenShareMap ( psm,colormap,precision )
  279.  
  280.     obtain a pensharemap's colors from a ColorMap.
  281.  
  282. ----------------------------------------------------------------------
  283. *********************************************************************/
  284.  
  285. BOOL ObtainPenShareMap (     struct PenShareMap *psm, struct ColorMap *cm, 
  286.                             ULONG precision )
  287. {
  288.     LONG baseColors, mainColors, histoColors;
  289.     struct PaletteExtra *pe;
  290.     UWORD numsharable = 0;
  291.  
  292.  
  293.     histoColors = QueryHistogram(psm->histogram,RND_NumColors);
  294.  
  295.     if(histoColors < 1)
  296.     {
  297.         return FALSE;
  298.     }
  299.  
  300.  
  301.     mainColors = MIN(histoColors,256);
  302.  
  303.  
  304.     if (pe = cm->PalExtra)
  305.     {
  306.         ObtainSemaphoreShared(&pe->pe_Semaphore);
  307.         numsharable = pe->pe_NFree;
  308.         baseColors = (DOUBLE)sqrt((DOUBLE)numsharable*(DOUBLE)8.0);
  309.         ReleaseSemaphore(&pe->pe_Semaphore);
  310.     }
  311.     else
  312.     {
  313.         baseColors = 0;
  314.     }
  315.  
  316.     baseColors = MIN(mainColors,baseColors);
  317.  
  318.     if(baseColors > 0)
  319.     {
  320.         if(!(psm->basepalette = CreatePalette(RND_HSType,HSTYPE_18BIT,
  321.                                 RND_RMHandler,psm->memhandler,TAG_DONE)))
  322.         {
  323.             ReleasePenShareMap(psm);
  324.             return FALSE;
  325.         }
  326.  
  327.         if(!(psm->basepentab = AllocRenderVec(psm->memhandler, baseColors)))
  328.         {
  329.             DeletePenShareMap(psm);
  330.             return FALSE;
  331.         }
  332.     }
  333.  
  334.     if(!(psm->mainpalette = CreatePalette(RND_HSType,HSTYPE_18BIT,
  335.                                 RND_RMHandler,psm->memhandler,TAG_DONE)))
  336.     {
  337.         DeletePenShareMap(psm);
  338.         return FALSE;
  339.     }
  340.  
  341.     if(!(psm->mainpentab = AllocRenderVec(psm->memhandler, mainColors)))
  342.     {
  343.         DeletePenShareMap(psm);
  344.         return FALSE;
  345.     }
  346.  
  347.     if(numsharable < THRESHOLD_SPAREMODE)
  348.     {
  349.  
  350.         if (baseColors > 0)
  351.         {
  352.             if (ExtractPalette(psm->histogram,psm->basepalette,baseColors,
  353.                         RND_NewPalette,TRUE,TAG_DONE) != EXTP_SUCCESS)
  354.             {
  355.                 DeletePenShareMap(psm);
  356.                 return FALSE;
  357.             }
  358.  
  359.             SortPalette(psm->basepalette,PALMODE_SIGNIFICANCE,
  360.                         RND_Histogram,psm->histogram,TAG_DONE);
  361.         }
  362.     }
  363.  
  364.     if (ExtractPalette(psm->histogram,psm->mainpalette,mainColors,
  365.                 RND_NewPalette,TRUE,TAG_DONE) != EXTP_SUCCESS)
  366.     {
  367.         DeletePenShareMap(psm);
  368.         return FALSE;
  369.     }
  370.  
  371.     SortPalette(psm->mainpalette,PALMODE_SIGNIFICANCE,
  372.                 RND_Histogram,psm->histogram,TAG_DONE);
  373.  
  374.     if(baseColors > 0)
  375.     {
  376.         if(ObtainPenTab(psm->basepalette,cm,psm->basepentab,baseColors,precision) == FALSE)
  377.         {
  378.             DeletePenShareMap(psm);
  379.             return FALSE;
  380.         }
  381.     }
  382.  
  383.     if (ObtainPenTab(psm->mainpalette,cm,psm->mainpentab,mainColors,precision) == FALSE)
  384.     {
  385.         DeletePenShareMap(psm);
  386.         return FALSE;
  387.     }
  388.  
  389.     psm->mainnumcolors = mainColors;
  390.     psm->basenumcolors = baseColors;
  391.     psm->colormap = cm;
  392.     return TRUE;
  393. }
  394.  
  395.  
  396. /*********************************************************************
  397. ----------------------------------------------------------------------
  398.  
  399.     DrawChunkyArray ( rastport,array,x,y,width,height )
  400.  
  401.     draw a chunky array to a rastport.
  402.  
  403. ----------------------------------------------------------------------
  404. *********************************************************************/
  405.  
  406. void DrawChunkyArray ( struct RastPort *rp, UBYTE *array, UWORD x, 
  407.                             UWORD y, UWORD width, UWORD height )
  408. {
  409.     struct RastPort trp;
  410.     struct BitMap *tbm;
  411.     
  412.     if (tbm = AllocBitMap(((width+15)>>4)<<4,1,rp->BitMap->Depth,0,rp->BitMap))
  413.     {
  414.         memcpy(&trp,rp,sizeof(struct RastPort));
  415.         trp.Layer = NULL;
  416.         trp.BitMap = tbm;
  417.         WritePixelArray8(rp,x,y,x+width-1,y+height-1,array,&trp);
  418.         FreeBitMap(tbm);
  419.     }
  420.  
  421. }
  422.